import pprint

class PhpClass:
	def __init__(self, name, extends = '', implements = '', root = ''):
		self.root = root
		if self.root == '':
			self.root = '/home/gregg/Source/ovenproject/'
		# process extends
		if extends != '':
			extends = ' extends ' + extends
		#process implements
		if implements != '':
			implements = ' implements ' + implements
		
		self.poss_vis = ['public', 'private', 'protected']
		self.contents = '<?php\n'
		self.contents += 'class ' + name + extends + implements + ' {\n'
		self.indent = '    '
		self.filename = ''
		self.name = name		
	
	# arr is a list
	def array(self, name, arr, visibility = None):
		if visibility == None or visibility not in self.poss_vis:
			visibility = 'var'
		
		arr_str = ''
		first = True
		for a in arr:
			if not first:
				arr_str += ', '
			arr_str += repr(a)
			first = False
			
		self.contents += self.indent + visibility + ' $' + name + ' = array(' + arr_str + ');\n'
		
	def variable(self, name, value, visibility = None):
		if visibility == None or visibility not in self.poss_vis:
			visibility = 'var'
		self.contents += self.indent + visibility + ' $' + name + ' = ' + repr(value) + ';\n'
	
	def function(self, name, params = [], visibility = None):
		# setup (var)visibility to have the keyword 'function' in the proper relation with it's visibility
		if visibility == None or visibility not in self.poss_vis:
			visibility = 'function '
		elif visibility in self.poss_vis:
			visibility += ' function '
		
		params_str = ''
		first = True
		for p in params:
			if not first:
				params_str += ', '
			params_str += '$' + p
			first = False

		self.contents += self.indent + visibility + name + '(' + params_str + ') {\n\n'
		self.contents += self.indent + '}\n'
		
	def newLine(self):
		self.contents += '\n'
	
	def outputToFile(self, is_class = True):
		# start the file stuff
		f = open(self.filename, 'w')
		if not is_class:
			f.write(self.contents + '\n')
		else:
			f.write(self.contents + '}\n')
		f.close()
	
	def toString(self):
		return self.contents + '}\n'
